﻿
using System;
using System.Linq.Expressions;

namespace HelperTools.Generics
{
	/// <summary>
	/// The Operator class provides easy access to the standard operators
	/// (addition, etc) for generic types, using type inference to simplify
	/// usage.
	/// </summary>
	public static class Operator
	{

		/// <summary>
		/// Indicates if the supplied value is non-null,
		/// for reference-types or Nullable&lt;T&gt;
		/// </summary>
		/// <returns>True for non-null values, else false</returns>
		public static bool HasValue<T>(T value)
		{
			return Operator<T>.NullOp.HasValue(value);
		}

		/// <summary>
		/// Increments the accumulator only
		/// if the value is non-null. If the accumulator
		/// is null, then the accumulator is given the new
		/// value; otherwise the accumulator and value
		/// are added.
		/// </summary>
		/// <param name="accumulator">The current total to be incremented (can be null)</param>
		/// <param name="value">The value to be tested and added to the accumulator</param>
		/// <returns>True if the value is non-null, else false - i.e.
		/// "has the accumulator been updated?"</returns>
		public static bool AddIfNotNull<T>(ref T accumulator, T value)
		{
			return Operator<T>.NullOp.AddIfNotNull(ref accumulator, value);
		}

		/// <summary>
		/// Evaluates unary negation (-) for the given type; this will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static T Negate<T>(T value)
		{
			return Operator<T>.Negate(value);
		}
		/// <summary>
		/// Evaluates bitwise not (~) for the given type; this will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static T Not<T>(T value)
		{
			return Operator<T>.Not(value);
		}
		/// <summary>
		/// Evaluates bitwise or (|) for the given type; this will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static T Or<T>(T value1, T value2)
		{
			return Operator<T>.Or(value1, value2);
		}
		/// <summary>
		/// Evaluates bitwise and (&amp;) for the given type; this will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static T And<T>(T value1, T value2)
		{
			return Operator<T>.And(value1, value2);
		}
		/// <summary>
		/// Evaluates bitwise xor (^) for the given type; this will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static T Xor<T>(T value1, T value2)
		{
			return Operator<T>.Xor(value1, value2);
		}
		/// <summary>
		/// Performs a conversion between the given types; this will throw
		/// an InvalidOperationException if the type T does not provide a suitable cast, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this cast.
		/// </summary>
		public static TTo Convert<TFrom, TTo>(TFrom value)
		{
			return Operator<TFrom, TTo>.Convert(value);
		}
		/// <summary>
		/// Evaluates binary addition (+) for the given type; this will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>        
		public static T Add<T>(T value1, T value2)
		{
			return Operator<T>.Add(value1, value2);
		}
		/// <summary>
		/// Evaluates binary addition (+) for the given type(s); this will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static TArg1 AddAlternative<TArg1, TArg2>(TArg1 value1, TArg2 value2)
		{
			return Operator<TArg2, TArg1>.Add(value1, value2);
		}
		/// <summary>
		/// Evaluates binary subtraction (-) for the given type; this will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static T Subtract<T>(T value1, T value2)
		{
			return Operator<T>.Subtract(value1, value2);
		}
		/// <summary>
		/// Evaluates binary subtraction(-) for the given type(s); this will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static TArg1 SubtractAlternative<TArg1, TArg2>(TArg1 value1, TArg2 value2)
		{
			return Operator<TArg2, TArg1>.Subtract(value1, value2);
		}
		/// <summary>
		/// Evaluates binary multiplication (*) for the given type; this will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static T Multiply<T>(T value1, T value2)
		{
			return Operator<T>.Multiply(value1, value2);
		}
		/// <summary>
		/// Evaluates binary multiplication (*) for the given type(s); this will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static TArg1 MultiplyAlternative<TArg1, TArg2>(TArg1 value1, TArg2 value2)
		{
			return Operator<TArg2, TArg1>.Multiply(value1, value2);
		}
		/// <summary>
		/// Evaluates binary division (/) for the given type; this will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static T Divide<T>(T value1, T value2)
		{
			return Operator<T>.Divide(value1, value2);
		}
		/// <summary>
		/// Evaluates binary division (/) for the given type(s); this will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static TArg1 DivideAlternative<TArg1, TArg2>(TArg1 value1, TArg2 value2)
		{
			return Operator<TArg2, TArg1>.Divide(value1, value2);
		}
		/// <summary>
		/// Evaluates binary equality (==) for the given type; this will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static bool Equal<T>(T value1, T value2)
		{
			return Operator<T>.Equal(value1, value2);
		}
		/// <summary>
		/// Evaluates binary inequality (!=) for the given type; this will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static bool NotEqual<T>(T value1, T value2)
		{
			return Operator<T>.NotEqual(value1, value2);
		}
		/// <summary>
		/// Evaluates binary greater-than (&gt;) for the given type; this will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static bool GreaterThan<T>(T value1, T value2)
		{
			return Operator<T>.GreaterThan(value1, value2);
		}
		/// <summary>
		/// Evaluates binary less-than (&lt;) for the given type; this will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static bool LessThan<T>(T value1, T value2)
		{
			return Operator<T>.LessThan(value1, value2);
		}
		/// <summary>
		/// Evaluates binary greater-than-on-eqauls (&gt;=) for the given type; this will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static bool GreaterThanOrEqual<T>(T value1, T value2)
		{
			return Operator<T>.GreaterThanOrEqual(value1, value2);
		}
		/// <summary>
		/// Evaluates binary less-than-or-equal (&lt;=) for the given type; this will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static bool LessThanOrEqual<T>(T value1, T value2)
		{
			return Operator<T>.LessThanOrEqual(value1, value2);
		}
		/// <summary>
		/// Evaluates integer division (/) for the given type; this will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary><remarks>
		/// This operation is particularly useful for computing averages and
		/// similar aggregates.
		/// </remarks>
		public static T DivideInt32<T>(T value, int divisor)
		{
			return Operator<int, T>.Divide(value, divisor);
		}
	}
	/// <summary>
	/// Provides standard operators (such as addition) that operate over operands of
	/// different types. For operators, the return type is assumed to match the first
	/// operand.
	/// </summary>
	/// <seealso cref="Operator&lt;T&gt;"/>
	/// <seealso cref="Operator"/>
	public static class Operator<TValue, TResult>
	{
		/// <summary>
		/// Returns a delegate to convert a value between two types; this delegate will throw
		/// an InvalidOperationException if the type T does not provide a suitable cast, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this cast.
		/// </summary>
		public static Func<TValue, TResult> Convert { get; }

		static Operator()
		{
			Convert = ExpressionUtil.CreateExpression<TValue, TResult>(body => Expression.Convert(body, typeof(TResult)));
			Add = ExpressionUtil.CreateExpression<TResult, TValue, TResult>(Expression.Add, true);
			Subtract = ExpressionUtil.CreateExpression<TResult, TValue, TResult>(Expression.Subtract, true);
			Multiply = ExpressionUtil.CreateExpression<TResult, TValue, TResult>(Expression.Multiply, true);
			Divide = ExpressionUtil.CreateExpression<TResult, TValue, TResult>(Expression.Divide, true);
		}

		/// <summary>
		/// Returns a delegate to evaluate binary addition (+) for the given types; this delegate will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static Func<TResult, TValue, TResult> Add { get; }

		/// <summary>
		/// Returns a delegate to evaluate binary subtraction (-) for the given types; this delegate will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static Func<TResult, TValue, TResult> Subtract { get; }

		/// <summary>
		/// Returns a delegate to evaluate binary multiplication (*) for the given types; this delegate will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static Func<TResult, TValue, TResult> Multiply { get; }

		/// <summary>
		/// Returns a delegate to evaluate binary division (/) for the given types; this delegate will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static Func<TResult, TValue, TResult> Divide { get; }
	}

	/// <summary>
	/// Provides standard operators (such as addition) over a single type
	/// </summary>
	/// <seealso cref="Operator"/>
	/// <seealso cref="Operator&lt;TValue,TResult&gt;"/>
	public static class Operator<T>
	{
		internal static INullOp<T> NullOp { get; }

		/// <summary>
		/// Returns the zero value for value-types (even full Nullable&lt;TInner&gt;) - or null for reference types
		/// </summary>
		public static T Zero { get; }

		/// <summary>
		/// Returns a delegate to evaluate unary negation (-) for the given type; this delegate will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static Func<T, T> Negate { get; }

		/// <summary>
		/// Returns a delegate to evaluate bitwise not (~) for the given type; this delegate will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static Func<T, T> Not { get; }

		/// <summary>
		/// Returns a delegate to evaluate bitwise or (|) for the given type; this delegate will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static Func<T, T, T> Or { get; }

		/// <summary>
		/// Returns a delegate to evaluate bitwise and (&amp;) for the given type; this delegate will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static Func<T, T, T> And { get; }

		/// <summary>
		/// Returns a delegate to evaluate bitwise xor (^) for the given type; this delegate will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static Func<T, T, T> Xor { get; }

		/// <summary>
		/// Returns a delegate to evaluate binary addition (+) for the given type; this delegate will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static Func<T, T, T> Add { get; }

		/// <summary>
		/// Returns a delegate to evaluate binary subtraction (-) for the given type; this delegate will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static Func<T, T, T> Subtract { get; }

		/// <summary>
		/// Returns a delegate to evaluate binary multiplication (*) for the given type; this delegate will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static Func<T, T, T> Multiply { get; }

		/// <summary>
		/// Returns a delegate to evaluate binary division (/) for the given type; this delegate will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static Func<T, T, T> Divide { get; }


		/// <summary>
		/// Returns a delegate to evaluate binary equality (==) for the given type; this delegate will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static Func<T, T, bool> Equal { get; }

		/// <summary>
		/// Returns a delegate to evaluate binary inequality (!=) for the given type; this delegate will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static Func<T, T, bool> NotEqual { get; }

		/// <summary>
		/// Returns a delegate to evaluate binary greater-then (&gt;) for the given type; this delegate will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static Func<T, T, bool> GreaterThan { get; }

		/// <summary>
		/// Returns a delegate to evaluate binary less-than (&lt;) for the given type; this delegate will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static Func<T, T, bool> LessThan { get; }

		/// <summary>
		/// Returns a delegate to evaluate binary greater-than-or-equal (&gt;=) for the given type; this delegate will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static Func<T, T, bool> GreaterThanOrEqual { get; }

		/// <summary>
		/// Returns a delegate to evaluate binary less-than-or-equal (&lt;=) for the given type; this delegate will throw
		/// an InvalidOperationException if the type T does not provide this operator, or for
		/// Nullable&lt;TInner&gt; if TInner does not provide this operator.
		/// </summary>
		public static Func<T, T, bool> LessThanOrEqual { get; }

		static Operator()
		{
			Add = ExpressionUtil.CreateExpression<T, T, T>(Expression.Add);
			Subtract = ExpressionUtil.CreateExpression<T, T, T>(Expression.Subtract);
			Divide = ExpressionUtil.CreateExpression<T, T, T>(Expression.Divide);
			Multiply = ExpressionUtil.CreateExpression<T, T, T>(Expression.Multiply);

			GreaterThan = ExpressionUtil.CreateExpression<T, T, bool>(Expression.GreaterThan);
			GreaterThanOrEqual = ExpressionUtil.CreateExpression<T, T, bool>(Expression.GreaterThanOrEqual);
			LessThan = ExpressionUtil.CreateExpression<T, T, bool>(Expression.LessThan);
			LessThanOrEqual = ExpressionUtil.CreateExpression<T, T, bool>(Expression.LessThanOrEqual);
			Equal = ExpressionUtil.CreateExpression<T, T, bool>(Expression.Equal);
			NotEqual = ExpressionUtil.CreateExpression<T, T, bool>(Expression.NotEqual);

			Negate = ExpressionUtil.CreateExpression<T, T>(Expression.Negate);
			And = ExpressionUtil.CreateExpression<T, T, T>(Expression.And);
			Or = ExpressionUtil.CreateExpression<T, T, T>(Expression.Or);
			Not = ExpressionUtil.CreateExpression<T, T>(Expression.Not);
			Xor = ExpressionUtil.CreateExpression<T, T, T>(Expression.ExclusiveOr);

			Type typeT = typeof(T);
			if (typeT.IsValueType && typeT.IsGenericType && (typeT.GetGenericTypeDefinition() == typeof(Nullable<>)))
			{
				// get the *inner* zero (not a null Nullable<TValue>, but default(TValue))
				Type nullType = typeT.GetGenericArguments()[0];
				Zero = (T)Activator.CreateInstance(nullType);
				NullOp = (INullOp<T>)Activator.CreateInstance(
						  typeof(StructNullOp<>).MakeGenericType(nullType));
			}
			else
			{
				Zero = default(T);
				if (typeT.IsValueType)
				{
					NullOp = (INullOp<T>)Activator.CreateInstance(
						 typeof(StructNullOp<>).MakeGenericType(typeT));
				}
				else
				{
					NullOp = (INullOp<T>)Activator.CreateInstance(
						 typeof(ClassNullOp<>).MakeGenericType(typeT));
				}
			}
		}

	}
}
